/* common Javascript in Pages */
fc = {
	 $id:function(a){return document.getElementById(a);}
	 //exsample
	 //http:www.google.co.jp/index.html -> index.html
	,getfile:function(a){return a.match(".+/(.+?)$")[1];}
	 //http:www.google.co.jp/index.html -> index
	,getfilen:function(a){return a.match(".+/(.+?)\.[a-z]+$"[1]);}
	,cknull:function(a){return(a!=null&&a!=undefined&&a!='')?true:false;}
	//"this is {0}.Hello {1}"{0}=hoge1,{1}=hoge2 -> "this is hoge1.Hello hoge2"
	,format:function(a,b){
		for(var i in b)a=a.replace(new RegExp('\\{'+i+'\\}','gi'),b[i]);
		return a;
	}
	//get length of associative array
	,getlenAary:function(a){var b=0;for(var c in a){b++;}return b;}
	,qc:['"','&','<','>',' ','©']
	,cq:['&quot;','&amp;','&lt;','&gt;','&nbsp;','&copy;']
};



var fitem=(function(a){this.init(a)});
fitem.prototype={
	 id:null
	,img:null
	,price:null
	,brand:null
	,uptime:null
	,info:null
	,name:null
	,store:null
	,el:null
	,format:'<div class="content" search="correct"><img class="itemimg" src="{0}" onmousedown="return false"/><div class="store">{1}</div><div class="brand">{2}</div></div>'
	,init:(function(a){
		this.set(a);
		//must check
		if(!fc.cknull(this.id))return null;
		this.makeEl();
	})
	,set:(function(a){
		if(fc.cknull(a.id))this.id=a.id;
		if(fc.cknull(a.img))this.img=a.img;
		if(fc.cknull(a.price)){this.price=a.price;}
		if(fc.cknull(a.brand)){this.brand=a.brand;}
		if(fc.cknull(a.uptime)){this.uptime=a.uptime;}
		if(fc.cknull(a.store)){this.store=a.store;}
		if(fc.cknull(a.info)){this.info=a.info;}
		if(fc.cknull(a.name)){this.name=a.name;}
	})
	,makeEl:(function(){
		this.el=$(fc.format(this.format,[this.img,this.store,this.brand]));
		var a=this;
		//set event
		//case : image onload error
		
		$(this.el).find('img.itemimg').error(function(){
			a.el.remove();
			if(fitems){delete fitems[a.id];}
			a=null;
		});
		
		this.el.dblclick(function(){
			$('#itemdetail').show();
		});
	})
	,appendEl:(function(a,b){
		// a is target element or id name
		if(!fc.cknull(a)){return null;}
		// case : a is string(id name)
		// else case : a is object(element)
		var c=(typeof(a)=='string')?$('#'+a):$(a);
		if(b===true){
			c.prepend(this.el);
		}else{
			c.append(this.el);
		}
	})
};

var itemdetail=(function(){this.init();});
itemdetail.prototype={
	 el:null
	,format: '<div id="item_detail"'
			+''
	,init:function(){
		var a=this;
		this.el=$('#itemdetail');
		// set event
		$('#itemdetail').click((function(){
			var b=true;
			$('#itemdetail .content').click(function(){b=false;});
			return (function(){
				if(!b){b=true;return false;}
				a.el.hide();
				$('body').css({'overflow-y':'auto'});
			});
		})());
	}
	,show:function(a){
		$('body').css({'overflow-y':'hidden'});
		var src=$(a).find('.itemimg').attr('src');
		this.el.find('.item img').attr('src',src);
		this.el.show();
	}
	,setAddEvent:function(a){
		var b=this;
		//a is jquery object or element object
		a.el.dblclick(function(){
			b.show(this);
		});
	}
};
